<?php
/**
 * 职位搜索,只为职位招聘使用
 */

apf_require_class('APF_Controller');
apf_require_class('Bll_Hr_Job');
apf_require_class('Const_Tip');
apf_require_class('Const_BaseInfo');
apf_require_class('Bll_Hr_UserRelation');
apf_require_class('Bll_Hr_OutInterface');
apf_require_class('Bll_Hr_User');
apf_require_class('Const_User');
class Hr_Ajax_SearchJobRecruitController extends APF_Controller {

    public function handle_request() {
        $params = APF::get_instance()->get_request()->get_parameters();
        $kw = $params['name'];
        $status=0;

        if(empty($kw)){
            $this->json_encode($status,Const_Tip::ERROR_PARAM);
        }
        if( mb_strlen($kw,'UTF-8') < 2){
            $this->json_encode(1,array());
        }
        $job_bll = new Bll_Hr_Job();
        $data = $job_bll->search_job($kw);
        if(empty($data)){
            $this->json_encode(1,array());
        }
        $result = array();
        foreach($data as $val){
            $result[$val['job_id']] = array(
                'job_id'=>$val['job_id'],
                'job_name'=>$val['position_name'].' - '.$val['dep_info']['city_name'].$val['dep_info']['bu_name'].$val['dep_info']['function_name'].'('.$val['dep_info']['department_name'].')',
                'city_id'=>$val['dep_info']['city_id'],
                'city_name'=>$val['dep_info']['city_name'],
                'plan_num'=>intval($val['plan_num']),
                //'fact_num'=>intval($val['fact_num']),
                'fact_num'=>0
                );
        }
        $job_ids = array_keys($result);
        $hroa_bll = new Bll_Hr_OutInterface();
        $job_list = $hroa_bll->get_job_user_list($job_ids);
        if(!empty($job_list)){
            foreach($job_list as $job_id=>$val){
                $result[$job_id]['fact_num'] = intval($val['fact_num']);
            }
        }
        /*
        $work_bll = new Bll_Hr_UserRelation();
        $work_data = $work_bll->get_job_user_list($job_ids);
        $uids = array();
        foreach($work_data as  $val){
            $uids[$val['user_id']] = $val['job_id'];
        }
        $uids_list = array_keys($uids);

        $user_bll = new Bll_Hr_User();
        $user_list = $user_bll->get_user_by_ids($uids_list);
        foreach($user_list as $val){
            if($val['job_status']==2){
                continue;
            }
            $jobid = $uids[$val['user_id']];
            //只统计正式和试用
            if($val['user_type']==Const_User::USER_TYPE_TRY_STAFF || $val['user_type']==Const_User::USER_TYPE_FORMAL_STAFF){
                $result[$jobid]['fact_num'] +=1;
            }
        }*/
        $status = 1;
        $this->json_encode($status,$result);
    }
    /**
     *
     * @param int $status 0错误 1成功
     * @param unknown_type $res
     */
    public function json_encode($status,$res){
        $result = array(
                'errcode'=>$status
        );
        if($status){
            $result['data']=$res;
        }else{
            $result['msg']=$res;
        }
        echo json_encode($result);
        exit();
    }
}